﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using Telerik.Web.UI;
using HCFramework.Product.PropertyManage;

namespace HCFramework.Product.FilterManage
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ChooseProperty1.LoadTreeData();
                ChooseProperty1.SelectedPropertyID = M.UrlQuery.propertyID;
                ChoosePropertyCopyFrom.LoadTreeData();
            }
        }

        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.PropertyFilters
                         where M.UrlQuery.propertyID.HasValue ? M.UrlQuery.propertyID == a.ProductPropertyID : false
                         select a;
            result = result.OrderBy(ik => ik.OrderDisplay);

            RadGridMain.DataSource = result;
            RadGridMain.DataBind();
        }

        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Them")
            {
                TextBox txtValue = e.Item.FindControl("txtValue") as TextBox;
                TextBox txtOrderDisplay = e.Item.FindControl("txtOrderDisplay") as TextBox;

                PropertyFilter p = new PropertyFilter();
                p.ProductPropertyID = M.UrlQuery.propertyID.Value;
                p.Value = txtValue.Text;
                p.OrderDisplay = int.Parse(txtOrderDisplay.Text);

                C.PropertyFilterController.Insert(p);
            }
        }

        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem gridItem in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)gridItem;
                int id = int.Parse(dataItem["PropertyFilterID"].Text);

                C.PropertyFilterController.Delete(id);
            }
        }

        protected void LinkButtonUpdate_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGridMain.Items)
            {
                int id = int.Parse(item["PropertyFilterID"].Text);
                TextBox txtValue = item.FindControl("txtValue") as TextBox;
                TextBox txtOrderDisplay = item.FindControl("txtOrderDisplay") as TextBox;

                HCDataContext db = new HCDataContext();
                PropertyFilter p = C.PropertyFilterController.GetByID(id, db);
                p.Value = txtValue.Text;
                p.OrderDisplay = int.Parse(txtOrderDisplay.Text);

                C.PropertyFilterController.Update(p, db);
            }
        }

        protected void RadGridMain_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                //Guid id = Guid.Parse((e.Item as GridDataItem)["ProductPropertyID"].Text);
                //ProductProperty p = C.ProductPropertyController.GetByID(id);
            }
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            string url = Request.Url.AbsoluteUri;
            url = M.Http.SetPara(url, "PropertyID", ChooseProperty1.SelectedPropertyID.HasValue ? ChooseProperty1.SelectedPropertyID.Value.ToString() : "");
            Response.Redirect(url);
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            if (!ChooseProperty1.SelectedPropertyID.HasValue
                || !ChoosePropertyCopyFrom.SelectedPropertyID.HasValue) return;
            ProductProperty fromProp = C.ProductPropertyController.GetByID(ChoosePropertyCopyFrom.SelectedPropertyID.Value);
            foreach (PropertyFilter propertyFilter in fromProp.PropertyFilters)
            {
                PropertyFilter p = new PropertyFilter();
                p.ProductPropertyID = ChooseProperty1.SelectedPropertyID.Value;
                p.Value = propertyFilter.Value;
                p.OrderDisplay = propertyFilter.OrderDisplay;
                C.PropertyFilterController.Insert(p);
            }
        }
    }
}